package com.oinux.playhelper.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.oinux.playhelper.R;
import com.oinux.playhelper.app.BaseActivity;
import com.oinux.playhelper.service.MainService;
import com.oinux.playhelper.widget.MemoryBar;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class MainActivity extends BaseActivity implements OnClickListener {

	private static final String TAG = "main_activity";

	private Button appStatusBtn;
	private Button appStatisticsBtn;
	private Button settingBtn;
	private MemoryBar memeoryBar;

	private int progress;
	private boolean minus = true;
	private int number;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
		checkUpdate();
	}

	private void checkUpdate() {
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
			@Override
			public void onUpdateReturned(int updateStatus,
					UpdateResponse updateInfo) {
				switch (updateStatus) {
				case 0: // has update
					UmengUpdateAgent.showUpdateDialog(MainActivity.this,
							updateInfo);
					break;
				case 1: // has no update
					break;
				case 2: // none wifi
					break;
				case 3: // time out
					break;
				}
			}
		});
	}

	private void initData() {
		progress = appContext.getAvailMemory();
		if (progress > 0)
			memeoryBar.setProgress(100 - progress);
		else
			memeoryBar.setProgress(100);
		startService(new Intent(this, MainService.class));
	}

	private void initView() {
		appStatusBtn = (Button) findViewById(R.id.app_status);
		appStatusBtn.setOnClickListener(this);
		appStatisticsBtn = (Button) findViewById(R.id.statistics);
		appStatisticsBtn.setOnClickListener(this);
		settingBtn = (Button) findViewById(R.id.settings);
		settingBtn.setOnClickListener(this);
		memeoryBar = (MemoryBar) findViewById(R.id.memeory_status);
		memeoryBar.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == appStatusBtn) {
			startActivity(new Intent(this, AppStatus.class));
		} else if (v == memeoryBar) {
			appContext.releaseMemory();
			number = 100 - progress;
			memeoryBar.post(new LoopRunnable());
		} else if (v == settingBtn) {
			startActivity(new Intent(this, Settings.class));
		} else if (v == appStatisticsBtn) {
			startActivity(new Intent(this, AppStatistics.class));
		}
	}

	class LoopRunnable implements Runnable {

		@Override
		public void run() {
			if (number == 100 || minus) {
				minus = true;
				number--;
			}
			if (number == -1 || !minus) {
				minus = false;
				number++;
			}
			if (number <= 100 - progress) {
				memeoryBar.setProgress(number);
				memeoryBar.post(this);
			} else {
				minus = true;
				number = 0;
				restart();
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (appContext.isSwitchTheme()) {
			appContext.setSwitchTheme(false);
			restart();
		}
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
